<div id='sidebar'>
    <div id="accordion">
        <h3><a href="#">Content Manager</a></h3>
        <div id="content_tree" class="tree">
        </div>
        <h3><a href="#">File Manager</a></h3>
        <div id="file_tree" class="tree">
        </div>
    </div>
</div>
<div id='main_form'>
    <h1>Digitalus CMS</h1>
    <p>Welcome to your Digitalus CMS site. This dashboard will serve as the hub for your work.</p>
</div>

<div class='dialog' id='item_properties_dialog' title='Item Properties'>
    props
</div>

<div class='dialog' id='content_dialog' title='Content Editor'>
    <?php
        echo $this->formHidden('block_id');
        echo $this->formTextarea('content_editor', null, array('style' => 'width:900px;height:500px;'));
    ?>
    <p class="command_prompt">
       <?php
        echo $this->digUiSelectMacro('command');
        echo $this->formSubmit('run_command', 'Run');
       ?>
    </p>
</div>

<div class='dialog' id='select_item_type_dialog' title='Select a content type'>
    <p>Select the type of page.</p>
    <?= $this->formHidden('item_id');?>
    <p><?= $this->digCmsSelectItemType('item_type');?></p>
</div>

<div class="dialog" id="generic_dialog"></div>
<?php
$this->jQuery()->addJavascriptFile('/jquery/jquery.tree.min.js');
$this->jQuery()->addJavascriptFile('/jquery/plugins/jquery.tree.contextmenu.js');
$this->jQuery()->addJavascriptFile('/jquery/plugins/jquery.tree.themeroller.js');
$this->jQuery()->addJavascriptFile('/jquery/jquery.htmlClean.js');
$this->jQuery()->addJavascriptFile('/jquery/jquery.textile.js');
$this->jQuery()->onLoadCaptureStart()
?>
$("#tester").click(function(){
return false;
});
$.dig.tree = $("#content_tree").tree({
    lang: {
        new_node	: "new_item"
    },
    ui: {
        theme_name: 'digitalus_tree'
    },
    data : {
        type : "json",
        async : true,
        opts : {
            method : "POST",
            url : "/digCms/item/tree-data/start/<?= Dig_Service_Module::settings('dig', 'contentRootId');?>"
        }
    },
    plugins : {
        contextmenu : {
            items : {
                // we need to remove these and then add them with a new alias to sort them
                //todo rewrite the context menu to support sorting
                rename: false,
                create: false,
                remove: false,

                refresh : {
                    label	: "Refresh Node",
                    icon	: "syncronize",
                    visible	: function (NODE, TREE_OBJ) {
                        if(NODE.length != 1) return false;
                        return true;
                    },
                    action	: function (NODE, TREE_OBJ) {
                        TREE_OBJ.refresh( NODE );
                    },
                    separator_after : true
                },
                edit_item	:	{
                    label	: "Edit Item",
                    icon	: "edit",
                    visible	: function (NODE, TREE_OBJ) {
                        if(NODE.length != 1) return 0;
                    },
                    action	: function (NODE, TREE_OBJ) {
                        $.dig.ui.disable();
                        $('#main_form').load('/digCms/item/edit', {id: NODE.attr('id')}, function(){
                            $.dig.ui.enable();
                            $.dig.ui.init();
                            $.dig.editor.init();
                            $.initForm();
                        });
                    }

                },
                create_item : {
                    label	: "Add Child",
                    icon	: "create",
                    visible	: function (NODE, TREE_OBJ) {
                        if(NODE.length != 1) return 0;
                        return TREE_OBJ.check("creatable", NODE);
                    },
                    action	: function (NODE, TREE_OBJ) {
                        TREE_OBJ.create({
                            new_name: 'tester'
                        }, TREE_OBJ.get_node(NODE[0]));
                    }
                },
                remove_item : {
                    label	: "Delete",
                    icon	: "remove",
                    visible	: function (NODE, TREE_OBJ) {
                        var ok = true;
                        $.each(NODE, function () {
                            if(TREE_OBJ.check("deletable", this) == false) {
                                ok = false;
                                return false;
                            }
                        });
                        return ok;
                    },
                    action	: function (NODE, TREE_OBJ) {
                        var item = NODE;
                        var pageName = item.text();
                        if(confirm("Are you sure you want to permanently delete " + pageName + "?")) {
                            $.post('/digCms/item/delete', {id: NODE.attr('id')}, function(){
                                $.each(NODE, function () {
                                    TREE_OBJ.remove(this);
                                });
                            });
                        }
                    },
                    separator_after : true
                },
                item_properties : {
                    label	: "Properties",
                    icon	: "properties",
                    visible	: function (NODE, TREE_OBJ) {
                        if(NODE.length != 1) return false;
                        return true;
                    },
                    action	: function (NODE, TREE_OBJ) {
                        $.post('/digCms/item/properties', {id: NODE.attr('id')}, function(response){
                            itemPropertiesDialog.html(response).dialog('open');
                        });
                    }
                },
                item_type : {
                    label	: "Set Item Type",
                    icon	: "item_type",
                    visible	: function (NODE, TREE_OBJ) {
                        if(NODE.length != 1) return false;
                        return true;
                    },
                    action	: function (NODE, TREE_OBJ) {
                        $.post('/digCms/item/json-details', {id: NODE.attr('id')}, function(item) {
                            $.itemTypeDialog.find('input#item_id').val(item.id);
                            $.itemTypeDialog.find('select#item_type').val(item.namespace);
                            $.itemTypeDialog.dialog('open');
                        }, 'json');
                    }
                },
            }
        }
    },
    callback: {
        onmove: function(NODE, REF_NODE, TYPE, TREE_OBJ, RB) {
            var data = {
                id	: $(NODE).attr('id'),
                ref	: $(REF_NODE).attr('id'),
                rel	: TYPE
            }
            $.post('/digCms/item/move', data, function(){
                // confirmation
                });
        },
        oncreate: function(NODE, REF_NODE, TYPE, TREE_OBJ, RB) {
            var data = {
                ref	: $(REF_NODE).attr('id'),
                rel	: TYPE
            }
            $.post('/digCms/item/create', data, function(response){
                $(NODE).attr('id', response['id']);
                $(NODE).addClass(response.namespace);
                $(NODE).find('a:first').html('<ins></ins> ' + response.name);
                $.dig.ui.disable();
                $('#main_form').load('/digCms/item/edit', {id: response.id}, function(){
                    $.dig.ui.enable();
                    $.dig.ui.init();
                    $.dig.editor.init();
                    $.initForm();
                });
            }, 'json');
            return false;
        },
        beforerename: function() {
            return false;
        },
        themeroller : {},
        cookie		: { }
    }

});

$("#file_tree").tree({
    lang: {
        new_node	: "new_item"
    },
    ui: {
        theme_name: 'digitalus_tree'
    },
    data : {
        type : "json",
        async : true,
        opts : {
            method : "POST",
            url : "/digFile/resource/tree-data/"
        }
    },
    plugins : {
        contextmenu : {
            items : {
                // we need to remove these and then add them with a new alias to sort them
                //todo rewrite the context menu to support sorting
                rename: false,
                create: false,
                remove: false,
                rename_item : {
                    label	: "Rename",
                    icon	: "rename",
                    visible	: function (NODE, TREE_OBJ) {
                        if(NODE.length != 1) return false;
                        return TREE_OBJ.check("renameable", NODE);
                    },
                    action	: function (NODE, TREE_OBJ) {
                        TREE_OBJ.rename(NODE);
                    }
                },
                remove_item : {
                    label	: "Discard",
                    icon	: "remove",
                    visible	: function (NODE, TREE_OBJ) {
                        var ok = true;
                        $.each(NODE, function () {
                            if(TREE_OBJ.check("deletable", this) == false) {
                                ok = false;
                                return false;
                            }
                        });
                        return ok;
                    },
                    action	: function (NODE, TREE_OBJ) {
                        var item = NODE;
                        var pageName = item.find('a:first').text();
                        if(confirm("Are you sure you want to send " + pageName + " to the trash?")) {
                            $.post('/digFile/resource/discard', {id: NODE.attr('id')}, function(){
                                $.each(NODE, function () {
                                    TREE_OBJ.remove(this);
                                });
                            });
                        }
                    },
                    separator_after : true
                },
                upload_file : {
                    label	: "Upload File",
                    icon	: "upload",
                    visible	: function (NODE, TREE_OBJ) {
                        if(NODE.length != 1) return 0;
                        if(NODE.attr('type') == 'dir') {
                            return true;
                        } else {
                            return false;
                        }
                    },
                    action	: function (NODE, TREE_OBJ) {
                        alert('upload method not implemented');
                    }
                },
                create_folder : {
                    label	: "Add Folder",
                    icon	: "add_folder",
                    visible	: function (NODE, TREE_OBJ) {
                        if(NODE.length != 1) return 0;
                        if(NODE.attr('type') == 'dir') {
                            return true;
                        } else {
                            return false;
                        }
                    },
                    action	: function (NODE, TREE_OBJ) {
                        TREE_OBJ.create({
                            new_name: 'tester'
                        }, TREE_OBJ.get_node(NODE[0]));
                    }
                }
            }
        }
    },
    callback: {
        onmove: function(NODE, REF_NODE, TYPE, TREE_OBJ, RB) {
            var id = $(NODE).attr('id');
            if(RB == 'inside') {
                var path = $(REF_NODE).attr('id');
            } else {
                var path = $(REF_NODE).closest('li.DigFile_Model_Symlink').attr('id');
            }
            $.post('/digFile/resource/move', {id: id, path: path}, function(){
                // confirmation
                });
        },
        oncreate: function(NODE, REF_NODE, TYPE, TREE_OBJ, RB) {
            var data = {
                path	: $(REF_NODE).attr('id'),
                rel	: TYPE
            }
            $.post('/digFile/resource/create', data, function(response){
                $(NODE).attr('id', response['id']);
                $(NODE).attr('type', response['type']);
                $(NODE).attr('ext', response['ext']);
                $(NODE).addClass(response.namespace);
            }, 'json');
            return false;
        },
        onrename: function(NODE, LANG, TREE_OBJ) {
            var item = $(NODE);
            var name = item.find('a:first').text();
            $.post('/digFile/resource/rename', {
                id : item.attr('id'),
                name : jQuery.trim(name)
            }, function(response){
                item.find('a:first').html('<ins></ins> ' + response.name);
            }, 'json');
        },
        themeroller : {},
        cookie		: { }
    }

});

$('#content_tree a').live('dblclick', function() {
    $.dig.ui.disable();
    $('#main_form').load('/digCms/item/edit', {id: $(this).closest('li').attr('id')}, function(){
        $.dig.ui.enable();
        $.dig.ui.init();
        $.dig.editor.init();
        $.initForm();
    });
} );
$.dialog = $('#generic_dialog').dialog({autoOpen: false});

$('#file_tree a').live('dblclick', function() {
    var resource = $(this).closest('li');
    var fileControl = $('#main_form input.filepath');
    if(fileControl.length == 0) {
        return false;
    } else if (fileControl.length == 1) {
        // TODO implement dialog when there is more than one control that accepts images
        if(fileControl.attr('accept').indexOf(resource.attr('ext')) >= 0) {
            fileControl.val(resource.attr('id'));
        } else {
            alert('Invalid file type');
        }
    } else {
        var html = '<input type="hidden" id="selected_file" value="' + resource.attr('id') + '" />';
        html += '<p>There are ' + fileControl.length + ' fields that accept files in your form. Please select one...</p>';
        html += '<ul id="select_file_location">';
        fileControl.each(function() {
            html += '<li><a href="#" rel="' + $(this).attr('id') + '">' +
                 $(this).parent().prev('dt').find('label').text() + '</a></li>';
        });
        html += '</ul>';
        $.dialog.html(html);

        $.dialog.dialog('option', 'title', 'Where do you want to insert this file?');
        $.dialog.dialog('open');
        $('#select_file_location a').click(function(){
            $("#" + $(this).attr('rel')).val($('#selected_file').val());
            $.dialog.dialog('close');
        });
    }
});

$("#accordion").accordion({ fillSpace: true });
var itemPropertiesDialog = $("#item_properties_dialog").dialog({
    autoOpen: false,
    width: 600
});



$.contentEditorDialog = $('#content_dialog').dialog({
    autoOpen: false,
    modal: true,
    width: 960,
    buttons: {
        'OK' : function(){
            var content = $(this).find('#content_editor').val();
            var block = $(this).find('#block_id').val();
            $('#' + block).val(content);
            $(this).find('#content_editor').val('');
            $(this).find('#block_id').val('');
            $(this).dialog('close');
        },
        'Cancel' : function(){
            $(this).find('#content_editor').val('');
            $(this).find('#block_id').val('');
            $(this).dialog('close');
        }
    }
});

$.initForm = function() {
    $('#main_form textarea').each(function () {
        var id = $(this).attr('id');
        var title = $(this).parent().prev('dt').find('label').text();
        $(this).after('<p><a class="content_dialog" href="#" title="' + title + '" rel="' + id + '">Expand</a></p>');
    });
    $("a.content_dialog").live('click', function() {
        var source = $('#' + $(this).attr('rel'));
        var content = source.val();
        $.contentEditorDialog.find('#content_editor').val(content).attr('class', source.attr('class'));
        $.contentEditorDialog.find('#block_id').val($(this).attr('rel'));
        $.contentEditorDialog.dialog('option', 'title', 'You are editing the ' + $(this).attr('title'));
        $.contentEditorDialog.dialog('open');
        return false;
    });
    $.contentEditorDialog.find('#run_command').live('click', function() {
        var editor = $.contentEditorDialog.find('#content_editor');
        var content = editor.val();
        var macroOutput = eval($('#command').val());
        if(typeof(macroOutput) != 'object') {
            editor.val(macroOutput);
        }
        return false;
    });
    var formOptions = {
        target: '#form_wrapper',
        success: function(){
            var id = $('#form_wrapper').find('input#id').val();
            var name = $('#form_wrapper').find('input#name').val();
            $('#content_tree').find('li#' + id).find('a:first').html('<ins></ins>' + name);
            $('.messages').remove();
            $.dig.ui.init();
            $.dig.editor.init();
            $.initForm();
            $.dig.ui.enable();
        },
        beforeSubmit: function(){
            $.dig.ui.disable();
        }
    }
    $('.ajaxForm').ajaxForm(formOptions);
}
$.itemTypeDialog = $('#select_item_type_dialog').dialog({
    autoOpen: false,
    modal: true,
    buttons: {
        'Save' : function(){
            var dialog = this;

            $.ajax({
                url:   '/digCms/item/set-item-type',
                async:  false,
                data:   {
                    id : $('#item_id').val(),
                    type : $(dialog).find('#item_type').val()
                },
                dataType:'json',
                success: function(response){
                    var treeNode = $('li#' + response.item.id);
                    var oldType = treeNode.attr('content_type');
                    treeNode.removeClass(oldType);
                    treeNode.addClass(response.item.namespace);
                    treeNode.attr('content_type', response.item.namespace);

                    if($('#id').val() == response.item.id) {
                        $('#main_form').load('/digCms/item/edit/id/' + response.item.id, null, function(){
                            $.dig.ui.init();
                            $.dig.editor.init();
                            $.initForm();
                            $.dig.ui.enable();
                        });
                    }
                },
                type:   'POST'
            });
            $(this).dialog('close');
        },
        'Cancel' : function(){
            $(this).dialog('close');
        }
    }
});

<?php $this->jQuery()->onLoadCaptureEnd();?>


